package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.WorkSpaceMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @author zhangyu
 * @data 2023/4/29 9:22
 */
@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private WorkSpaceMapper workSpaceMapper;
    @Override
    public BusinessDataVO getBusinessData() {
        //拿到今天的开始和截止时间
        LocalDateTime dayStart=LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime dayEnd=LocalDateTime.of(LocalDate.now(),LocalTime.MAX);
        //拿到今日的总营业额
        Double turnOver = workSpaceMapper.getTurnOver(dayStart, dayEnd);
        if(turnOver==null){
            turnOver=0.0;
        }
        //拿到今日有效订单数
        Integer orderCompleteCount = workSpaceMapper.getOrderCount(dayStart, dayEnd, Orders.COMPLETED);
        //获取今日订单总数
        Integer orderCount = workSpaceMapper.getOrderCount(dayStart, dayEnd,null);
        //获取今日订单完成率
        Double orderCompleteRate=orderCompleteCount.doubleValue()/orderCount;
        //获取平均客单价
        Double avgAmount;
        if(turnOver==0.0||orderCompleteCount==0){
            avgAmount=0.0;
        }else {
            avgAmount=turnOver/orderCompleteCount;
        }
        //获取今日新增用户数
        Integer newUserCount = workSpaceMapper.getNewUserCount(dayStart, dayEnd);
        return new BusinessDataVO(turnOver,orderCompleteCount,orderCompleteRate,avgAmount,newUserCount);

    }

    @Override
    public SetmealOverViewVO getSetMealStatus() {
        return new SetmealOverViewVO(workSpaceMapper.getSetMealStatus(1),workSpaceMapper.getSetMealStatus(0));
    }

    @Override
    public DishOverViewVO getDishStatus() {
        return new DishOverViewVO(workSpaceMapper.getDishStatus(1),workSpaceMapper.getDishStatus(0));
    }

    @Override
    public OrderOverViewVO getOrderStatus() {
        return new OrderOverViewVO(workSpaceMapper.getOrderStatus(2),workSpaceMapper.getOrderStatus(3),
                workSpaceMapper.getOrderStatus(5),workSpaceMapper.getOrderStatus(6),workSpaceMapper.getOrderStatus(null));
    }
}
